<?php

namespace App\Http\Controllers\admin;

use App\Model\AsdModel;
use Illuminate\Http\Request;
use App\Model\AdminLogsModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;


//客服中心
class AsdController extends Controller
{
	//客服中心
	public function index(){
		$asd_tel = AsdModel::where('type',1)->get();
		$asd_wec = AsdModel::where('type',2)->get();

		return view('admin.asd.index',compact('asd_tel','asd_wec'));
	}

	//添加客服
	public function add(){
		return view('admin.asd.add');
	}

	//添加
	public function insert(){
		DB::beginTransaction();
		try{
			if(AsdModel::where('type',request()->type)->where('phone',request()->phone)->first()){
				return ['code'=>0,'msg'=>'该手机号已存在'];
			}
			switch(request()->type){
				case '1':
					if(!request()->phone){
						return ['code'=>0,'msg'=>'请输入联系方式'];
					}
					AsdModel::do_add(1,request()->phone);
					break;

				case '2':
					if(!request()->phone){
						return ['code'=>0,'msg'=>'请输入微信账号'];
					}
					if(!request()->qrcode){
						return ['code'=>0,'msg'=>'请上传微信二维码'];
					}
					AsdModel::do_add(2,request()->phone,request()->qrcode);
					break;
			}
			$msg = '添加客服信息';
			AdminLogsModel::addLog($msg);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'添加失败'];
		}

	}

	//删除
	public function del(){
		DB::beginTransaction();
		try{
			$asd = AsdModel::where('id',request()->id)->first();
			if($asd->type == 2){
				@unlink(".".$asd->qrcode);
			}
			AsdModel::where('id',request()->id)->delete();
			$msg = '删除客服信息(id:'.request()->id.')';
			AdminLogsModel::addLog($msg);
			DB::commit();
			return ['code'=>1,'msg'=>'已删除'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}
}